<template>
  <div class="list-table">
    <el-form ref="form" label-width="120px" class="dataForm" style="left: 20px;right:5px;">
      <!-- 变更记录 -->
      <div class="area-header" style="margin-top: 10px;">
        <span class="area-header-title">{{ $t('股东变更记录') }}</span>
      </div>
      <el-table ref="checkTable" v-loading="loading" :data="companyHolderChangeList" tooltip-effect="dark" border @selection-change="getCheckedDataVS">
        <el-table-column
          v-model="checkedChange"
          type="selection"
          width="55"
        />
        <el-table-column :label="$t('变更时间')" align="center" prop="changeDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.changeDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('总股本') + $t('金额单位')" align="center" prop="totalMoney">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.totalMoney, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('总股数(万股)')" align="center" prop="totalNum">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.totalNum) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('阶段')" align="center" prop="changeStageLabel" />
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['company:holder:remove']"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="area-header" style="margin-top: 10px;">
        <span class="area-header-title">{{ $t('股东对比') }}</span>
      </div>
      <div style="">
        <div class="change-vs">
          <div v-for="(item, index) in holderDataVSList" :key="index" class="change-list">
            <div class="holder-data">
              <div class="change-date">{{ parseTime(item.changeDate) }}</div>
              <div class="holder-list-tit">
                <p>{{ $t('股东名称') }}</p>
                <p>{{ $t('持股比例(%)') }}</p>
              </div>
              <div v-for="(holder, ind) in item.holders" :key="ind" class="holder-list">
                <span>{{ holder.holderName }}</span>
                <span>{{ holder.holderRatio }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import {
  listCompanyHolderChange,
  delCompanyHolderChange,
  getCheckedDataVS
} from '@/api/company/holder/companyHolderChange'
export default {
  data() {
    return {
      // 变更历史遮罩层
      loading: true,
      // 对比遮罩层
      loadingVS: false,
      // 查询参数
      queryParams: {
        companyId: this.$route.query.companyId
      },
      // 股东变更历史记录
      companyHolderChangeList: [],
      // 对比数据集合
      holderDataVSList: [],
      // 股东类型字典
      holderTypeOptions: [],
      // 是否字典
      yesNoOptions: [],
      // 选中的股东变更信息
      checkedChange: [],
      // 是否全选
      isAllChecked: false,
      // 选中的ID集合
      changeIds: ''
    }
  },
  watch: {
    checkedChange() {
      if (this.checkedChange.length === this.companyHolderChangeList.length) {
        this.isAllChecked = true
      } else {
        this.isAllChecked = false
      }
    }
  },
  created() {
    this.getDicts('company_person').then(response => {
      this.holderTypeOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.yesNoOptions = response.data
    })
    // this.getList()
  },
  methods: {
    /** 查询股东信息列表 */
    getList() {
      this.loading = true
      // 先查询所有的变更历史数据，默认选中当前最新的一条变更记录
      listCompanyHolderChange(this.queryParams).then(response => {
        this.companyHolderChangeList = response.rows
        if (this.companyHolderChangeList !== null && this.companyHolderChangeList.length > 0) {
          var index = 0
          // 默认全选，全部展示
          this.companyHolderChangeList.forEach(item => {
            if (index < 10) {
              this.checkedChange.push(item.id)
              this.$nextTick(function() {
                this.$refs.checkTable.toggleRowSelection(item, true)
              })
            }
            index = index + 1
          })
        }
        this.loading = false
      })
    },
    // 根据选中的ID，展示对比信息
    getCheckedDataVS(val) {
      this.changeIds = ''
      if (val !== null && val !== undefined) {
        val.forEach(item => {
          this.changeIds = this.changeIds + ',' + item.id
        })
        this.queryParams.checkedChangeIds = this.changeIds
        getCheckedDataVS(this.queryParams).then(res => {
          this.holderDataVSList = res.data
        })
      }
    },
    // 股东类型字典
    holderTypeFormat(row) {
      return this.selectDictLabel(this.holderTypeOptions, row.holderType)
    },
    // 是否字典翻译
    yesNoFormat(row) {
      return this.selectDictLabel(this.yesNoOptions, row.whetherActualControl)
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delCompanyHolderChange(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    }
  }
}
</script>
